﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using BrokerBase.Lib.Policy;
using Telerik.Windows.Controls;

namespace Broker.View.PolicyView
{
    /// <summary>
    /// DutyActingForm.xaml 的交互逻辑
    /// </summary>
    public partial class DutyActingForm 
    {
        //private IScheme scheme;
        private SchemeActingForm schemeActingFrm;
        private ISchemePeriodActing spa;

        public bool IsFreeEdition =! LibGlobal.IsActive;
        private ControlTemplate freeEditionControlTemplate = Application.Current.Resources["FreeEditionDataControlTemplate"] as ControlTemplate;
        private DataTemplate freeEditionDataTemplate = Application.Current.Resources["FreeEditionDataTemplate"] as DataTemplate;

        public event SchemePeriodActingChangedHandler SchemePeriodActingChanged;

        public DutyActingForm(SchemeActingForm owner,ISchemePeriodActing spa)
        {
            InitializeComponent();
            this.Owner = owner;
            this.schemeActingFrm = owner;

            Loaded += new RoutedEventHandler(DutyActingForm_Loaded);
            schemeActingFrm.SchemePeriodActingChanged += new SchemePeriodActingChangedHandler(schemeActingFrm_SchemePeriodActingChanged);
            this.spa = spa;
        }

        void schemeActingFrm_SchemePeriodActingChanged(ISchemePeriodActing spa)
        {
            this.DataContext = null;
            this.DataContext = spa;

            if (spa != null && SchemePeriodActingChanged != null)
            {
                SchemePeriodActingChanged(spa);
            }
        }

        void DutyActingForm_Loaded(object sender, RoutedEventArgs e)
        {
            if (IsFreeEdition)
            {
                HideData();
            }

            if (!IsInitTab)
            {
                InitTab();
            }
        }


        private void HideData()
        {
            txtValue.Template = freeEditionControlTemplate;
            txtTotalValue.Template = freeEditionControlTemplate;
        }

        private void btnExit_Click(object sender, RoutedEventArgs e)
        {
            Close();
        }

        private bool IsInitTab
        {
            get { return tabPolicy.Items.Count > 0; }
        }

        private void InitTab()
        {
            try
            {
                foreach (IPolicyActing policyActing in spa.SchemeActing.PolicyActings)
                {

                    DutyDetailPage page = new DutyDetailPage(this, policyActing);

                    if (IsFreeEdition)
                    {
                        page.grdDetail.Columns["colValue"].CellTemplate = freeEditionDataTemplate;
                        page.grdDetail.Columns["colSumValue"].CellTemplate = freeEditionDataTemplate;
                    }

                    Frame frame = new Frame();
                    frame.Content = page;

                    RadTabItem tabItem = new RadTabItem();
                    tabItem.Header = policyActing.Policy.Product.Name;
                    tabItem.Content = frame;

                    tabPolicy.Items.Add(tabItem);

                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}
